package com.itheima;

import io.minio.*;
public class TestDemo {
    public static void main(String[] args) {
        /*
        构造minio的客户端
         */
        MinioClient minioClient = MinioClient.builder().endpoint("http://192.168.174.130:9000").
                credentials("minioadmin", "minioadmin").build();
        /*
        根据客户端来创建桶
         */
        try {
            boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket("test").build());
            if (!found) {//表示桶不存在，则创建桶
                minioClient.makeBucket(MakeBucketArgs.builder().bucket("test").build());
                //设置hello-minio桶的访问权限
                String policy = "{\n" +
                        "    \"Version\": \"2012-10-17\",\n" +
                        "    \"Statement\": [\n" +
                        "        {\n" +
                        "            \"Effect\": \"Allow\",\n" +
                        "            \"Principal\": {\n" +
                        "                \"AWS\": [\n" +
                        "                    \"*\"\n" +
                        "                ]\n" +
                        "            },\n" +
                        "            \"Action\": [\n" +
                        "                \"s3:GetObject\"\n" +
                        "            ],\n" +
                        "            \"Resource\": [\n" +
                        "                \"arn:aws:s3:::test/*\"\n" +
                        "            ]\n" +
                        "        }\n" +
                        "    ]\n" +
                        "}";
                minioClient.setBucketPolicy(SetBucketPolicyArgs.builder().bucket("test").config(policy).build());
            }else {
               System.out.println("Bucket 'hello-minio' already exists.");
            }
            //上传图片
            minioClient.uploadObject(UploadObjectArgs.builder().
                    bucket("test").object("zjh.jpg").
                    filename("H:\\schoolShixun\\test\\src\\main\\resources\\1.jpg").build());
            System.out.println("上传成功");
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}